//
//  GuideVc.swift
//  MikerShop
//
//  Created by eme on 2016/12/5.
//  Copyright © 2016年 eme. All rights reserved.
//

import UIKit
import RxSwift
import Alamofire
import SwiftyUserDefaults
import UtilCore
import RickonCore

class GuideVc: UIViewController {
    
    @IBOutlet weak var pageControl: UIPageControl!
    @IBOutlet weak var startButton: UIButton!
    internal var scrollView: UIScrollView!
    internal let disposeBag = DisposeBag()
    
    @IBOutlet weak var start_bottom: NSLayoutConstraint!
    
    internal let numOfPages = 4
    override func viewDidLoad() {
        super.viewDidLoad()
        let frame = self.view.bounds
        scrollView = UIScrollView(frame: frame)
        scrollView.isPagingEnabled = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.bounces = false
        scrollView.contentOffset = CGPoint.zero
        // 将 scrollView 的 contentSize 设为屏幕宽度的3倍(根据实际情况改变)
        scrollView.contentSize = CGSize(width: frame.size.width * CGFloat(numOfPages), height: frame.size.height)
        
        scrollView.delegate = self
        
        for index  in 0..<numOfPages {
           
            let imageView = UIImageView(image: UIImage(named: "GuideImage\(index + 1)", in: JonSnowCore.bundle, compatibleWith: nil))
            imageView.frame = CGRect(x: frame.size.width * CGFloat(index), y: 0, width: frame.size.width, height: frame.size.height)
            scrollView.addSubview(imageView)
        }
        self.view.insertSubview(scrollView, at: 0)
        // 给开始按钮设置圆角
        self.startButton.changeBorderColor(Theme.jonSnow.guide.start_btn_border, cornerRadius: 6.0)
        // 隐藏开始按钮
        startButton.alpha = 0.0
        self.startButton.rx.tap.subscribe(onNext: { ( _ ) in
            /// 表示已经登陆过 之后就不跳转引导界面了
            Defaults[.firstPage] = true
            _ = "loginsuccess".openURL()
        }).disposed(by: disposeBag)
        switch Device.size {
        case .Screen3_5Inch:
            self.start_bottom.constant = 80
        case .Screen4Inch:
            self.start_bottom.constant = 80
        case .Screen4_7Inch:
            self.start_bottom.constant = 95
        case .Screen5_5Inch:
            self.start_bottom.constant = 100
        case .Screen7_9Inch:
            self.start_bottom.constant = 100
        case .Screen9_7Inch:
            self.start_bottom.constant = 100
        case .Screen12_9Inch:
            self.start_bottom.constant = 100
        default:
            self.start_bottom.constant = 100
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
// MARK: - UIScrollViewDelegate
extension GuideVc: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let offset = scrollView.contentOffset
        // 随着滑动改变pageControl的状态
        pageControl.currentPage = Int(offset.x / view.bounds.width)
        
        // 因为currentPage是从0开始，所以numOfPages减1
        if pageControl.currentPage == numOfPages - 1 {
            UIView.animate(withDuration: 0.5) {
                self.startButton.alpha = 1.0
            }
        } else {
            UIView.animate(withDuration: 0.2) {
                self.startButton.alpha = 0.0
            }
        }
    }
}
